package com.alinesno.cloud.initializr.starter.gateway.rest.cicd;

import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alinesno.cloud.common.facade.response.AjaxResult;
import com.alinesno.cloud.initializr.starter.dto.BuildDto;
import com.alinesno.cloud.initializr.starter.entity.BuildProjectParamsEntity;
import com.alinesno.cloud.initializr.starter.entity.ProjectInfoEntity;
import com.alinesno.cloud.initializr.starter.gateway.dto.BuildProjectParamsDto;
import com.alinesno.cloud.initializr.starter.seedgen.bean.ProjectInfoDto;
import com.alinesno.cloud.initializr.starter.service.IBuildProjectInfoService;
import com.alinesno.cloud.initializr.starter.service.IBuildProjectParamsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 发布cicd工程
 * 
 * @author WeiXiaoJin
 * @since 2021年6月27日 下午5:15:02
 */
@RestController
@RequestMapping("/api/deployment/cicd")
public class DeploymentCicdRest {

	private static final Logger log = LoggerFactory.getLogger(DeploymentCicdRest.class);

	@Autowired
	private IBuildProjectParamsService projectParamsService;

	@Autowired
	private IBuildProjectInfoService buildProjectInfoService;

	/**
	 * 发布构建
	 * 
	 * @param dto
	 * @return
	 */
	@PostMapping("publicProject")
	public AjaxResult publicProject(@RequestBody BuildProjectParamsDto info) {

		log.debug("build project : {}", info);

		Assert.hasLength(info.getProjectId(), "所属项目为空");

		QueryWrapper<BuildProjectParamsEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id", info.getProjectId());
		BuildProjectParamsEntity e = projectParamsService.getOne(wrapper);

		if (e == null) {
			e = new BuildProjectParamsEntity();
			BeanUtils.copyProperties(info, e);
			projectParamsService.save(e);
		} else {
			e = new BuildProjectParamsEntity();
			BeanUtils.copyProperties(info, e);
			e.setId(info.getId());
			projectParamsService.update(e);
		}

		return AjaxResult.success();
	}
	
	/**
	 * 构建工程包
	 */
	@GetMapping("/buildGitlab/{projectId}")
	public AjaxResult buildGitlab(@PathVariable String projectId) {

		ProjectInfoEntity entity = buildProjectInfoService.getById(projectId) ; 
		
		ProjectInfoDto dto = new ProjectInfoDto() ; 
		dto.setProjectId(entity.getId());
		dto.setNamespace("test-cicd") ; 
		dto.setProjectName(entity.getArtifactId());
		
		entity = buildProjectInfoService.uploadGitProject(dto) ;
		log.debug("entity = {}" , entity) ; 
		
		return entity!=null?AjaxResult.success() : AjaxResult.error() ; 
	}

	/**
	 * 构建状态
	 */
	@GetMapping("/buildDockerStatus/{projectId}")
	public AjaxResult buildDockerStatus(@PathVariable String projectId) {
		ProjectInfoEntity p = buildProjectInfoService.getById(projectId);
		log.debug("p:{}" , p);
		String jenkinsJobName = p.getJenkinsJobName(); 
	
		boolean b = buildProjectInfoService.isFinishJob(jenkinsJobName) ; 
		
		AjaxResult r =  AjaxResult.success() ; 
		r.put("isFinish", b) ; 
		
		return r ; 
	}

	/**
	 * 构建工程包
	 */
	@GetMapping("/buildDocker/{projectId}")
	public AjaxResult buildDocker(@PathVariable String projectId) {
		
		ProjectInfoEntity entity = buildProjectInfoService.getById(projectId) ; 
		boolean b = buildProjectInfoService.isFinishJob(entity.getJenkinsJobName()) ; 
		if(!b) {
			return AjaxResult.error("工程["+entity.getArtifactId()+"]已经在构建中") ; 
		}
		
		BuildDto dto = new BuildDto() ; 
		dto.setBranches("master");
		
		String jenkinsJobName = buildProjectInfoService.buildDocker(projectId, dto);

		return AjaxResult.success(jenkinsJobName);
	}
	
	/**
	 * 构建工程包
	 */
	@GetMapping("/buildK8S/{projectId}")
	public AjaxResult buildK8s(@PathVariable String projectId) {
	
		Assert.hasLength(projectId , "项目为空") ;
		
		QueryWrapper<BuildProjectParamsEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id", projectId) ; 
		BuildProjectParamsEntity params = projectParamsService.getOne(wrapper);

		buildProjectInfoService.publicK8S(projectId , params) ; 

		AjaxResult r = AjaxResult.success();
		return r ; 
	}


	/**
	 * 发布deployment
	 */
	@GetMapping("/publicDeployment")
	public AjaxResult publicDeployment(String projectId) {

		log.debug("project id = {}", projectId);

		buildProjectInfoService.publicDeployment(projectId , null);

		return AjaxResult.success();
	}

	/**
	 * 发布service
	 * 
	 * @param projectId
	 * @return
	 */
	@GetMapping("/publicService")
	public AjaxResult publicService(String projectId) {

		log.debug("project id = {}", projectId);

		buildProjectInfoService.publicService(projectId , null);

		return AjaxResult.success();
	}

	/**
	 * 发布ingress
	 * 
	 * @param projectId
	 * @return
	 */
	@GetMapping("/publicIngress")
	public AjaxResult publicIngress(String projectId) {

		log.debug("project id = {}", projectId);

		buildProjectInfoService.publicIngress(projectId , null);

		return AjaxResult.success();
	}

	/**
	 * 应用构建信息
	 * 
	 * @param projectId
	 * @return
	 */
	@GetMapping("projectBuildInfo/{projectId}")
	public AjaxResult projectBuildInfo(@PathVariable String projectId) {

		QueryWrapper<BuildProjectParamsEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("project_id", projectId);

		BuildProjectParamsEntity e = projectParamsService.getOne(wrapper);

		return AjaxResult.success(e);
	}

}
